<template>
  <el-dialog :title="tieleName" :visible.sync="dialogVisible" :close-on-click-modal="false"
             :close-on-press-escape="false" :before-close="handleClose" @close="$emit('update:visible',false)">
    <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
      <el-form-item label="书名" prop="name">
        <el-input v-model="editForm.name" auto-complete="off" placeholder="填写书名"></el-input>
      </el-form-item>
      <el-form-item label="图书分类" prop="book_cat_id">
        <el-select placeholder="图书分类" @change="changeSearchSelectFn(1)" v-model="first_id">
          <el-option :label="item.name" :value="index" v-for="(item, index) in tableList"
                     :key="index"></el-option>
        </el-select>
        <el-select placeholder="图书分类" @change="changeSearchSelectFn(2)" v-if="sonselectList" v-model="son_id">
          <el-option :label="item.name" :value="item.id" v-for="(item, index) in sonselectList"
                     :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="存放位置" prop="position">
        <el-input v-model="editForm.position" auto-complete="off" placeholder="填写存放位置"></el-input>
      </el-form-item>
      <el-form-item label="库存" prop="num">
        <el-input v-model="editForm.num" auto-complete="off" placeholder="填写库存"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="editForm.status">
          <el-radio label="1">开启借阅</el-radio>
          <el-radio label="0">禁止借阅</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="作者" prop="editor">
        <el-input v-model="editForm.editor" auto-complete="off" placeholder="填写库存"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="press">
        <el-input v-model="editForm.press" auto-complete="off" placeholder="填写价格"></el-input>
      </el-form-item>
      <el-form-item label="条形码" prop="barcode">
        <el-input v-model="editForm.barcode" auto-complete="off" placeholder="填写条形码"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click.native="resetForm" size="small">取消</el-button>
      <el-button type="primary" @click.native="editSubmit" size="small">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import api from '@/utils/api.js'

  export default {
    name: 'Dialog',
    props: ['visible', 'ditType', 'eidtOption', 'tableList'],
    watch: {
      visible () {
        this.dialogVisible = this.visible
        if (this.ditType === 1) {
          this.tieleName = '添加图书'
        } else {
          this.tieleName = '编辑图书'
          this.editForm.barcode = this.eidtOption.barcode
          this.editForm.editor = this.eidtOption.editor
          this.editForm.name = this.eidtOption.name
          this.editForm.position = this.eidtOption.position
          this.editForm.press = this.eidtOption.press
          this.editForm.status = this.eidtOption.status.toString()
          this.editForm.num = this.eidtOption.num
          this.editFormId = this.eidtOption.id
          this.editForm.book_cat_id = this.eidtOption.book_cat_id

          for (let i in this.tableList) {
            if (this.eidtOption.book_cat_id === Number(i)) {
              this.first_id = i
            } else {
              for (let j in this.tableList[i].child) {
                if (this.eidtOption.book_cat_id === Number(j)) {
                  this.first_id = i
                  this.son_id = j
                  this.sonselectList = this.eidtOption.book_cat_id[i].child
                }
              }
            }
          }
        }
      }
    },

  data () {
      return {
        tieleName: '',
        dialogVisible: false,
        title: null,
        sonselectList: null,
        first_id: '',
        son_id: '',
        editFormId: '',
        editForm: {
          barcode: '',
          editor: '',
          name: '',
          position: '',
          press: '',
          status: '1',
          num: '',
          book_cat_id: ''
        }, // 编辑界面数据
        editFormRules: {
          name: [
            {required: true, message: '请填写书名', trigger: 'blur'}
          ],
          position: [
            {required: true, message: '请填写图书存放位置', trigger: 'blur'}
          ],
          num: [
            {required: true, message: '请填写库存', trigger: 'blur'}
          ],
          editor: [
            {required: true, message: '请填写作者', trigger: 'blur'}
          ],
          press: [
            {required: true, message: '请填写价格', trigger: 'blur'}
          ],
          barcode: [
            {required: true, message: '请填写条形码', trigger: 'blur'}
          ]
        } // 表单验证不能为空规则
      }
  },

  created () {
    },

  methods: {
      // 提交数据
      editSubmit () {
        // 验证信息
        let _self = this
        this.$refs['editForm'].validate((valid) => {
          if (valid) {
            _self.submitBook()
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      // 提交新增，编辑书籍
      submitBook () {
        // 判断新增 编辑
        if (this.sonselectList && !this.son_id) {
          this.$message.error('请选择图书分类')
          return
        }
        let url_ = api.book_add
        let message = '添加成功'
        let data_ = this.editForm
        if (this.ditType === 2) {
          url_ = api.book_edit + this.editFormId
          message = '编辑成功'
          data_.id = this.editFormId
        }
        this.$post(url_, data_).then((response) => {
          if (response.code === 0) {
            this.$message({
              message: message,
              type: 'success'
            })
            this.dialogVisible = false
            this.$refs['editForm'].resetFields()
            this.editForm.book_cat_id = ''
            this.$emit('initFile')
          } else {
            console.log('请求失败')
          }
        }).catch((err) => {
          console.log(err)
        })
      },

      changeSearchSelectFn (option) {
        // option 1 顶级分类 2 二级分类
        if (option === 1) {
          this.son_id = ''
          this.sonselectList = this.tableList[this.first_id].child
          if (JSON.stringify(this.sonselectList) == '{}') {
            this.sonselectList = null
          }
          this.editForm.book_cat_id = this.first_id
        } else {
          this.editForm.book_cat_id = this.son_id
        }
      },

      resetForm () {

      },

      // 关闭弹窗之前执行
      handleClose () {
        this.dialogVisible = false
      }

    }
  }
</script>

